
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Message;

public class GenericMessageDAO extends AutoKeys implements
		org.jetnuke.dao.MessageDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.MessageModel#retrieveByPK(int)
	 */
	public Message retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("MessageModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Message s = new Message();
		
		if (rs.next()) {
			fillMessageFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.MessageModel#selectAll()
	 */
	public List selectAll()throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("MessageModel.selectAll"));
		
		ResultSet rs = p.executeQuery();
		List list = processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Message u = new Message();
			fillMessageFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	protected void fillMessageFromResultSet(Message u, ResultSet rs)throws Exception {
		u.setACTIVE(rs.getInt("ACTIVE"));
		u.setCONTENT(rs.getString("CONTENT"));
		u.setDATE(rs.getTimestamp("DATE"));
		u.setEXPIRE(rs.getInt("EXPIRE"));
		u.setMID(rs.getInt("MID"));
		u.setMLANGUAGE(rs.getString("MLANGUAGE"));
		u.setTITLE(rs.getString("TITLE"));
		u.setVIEW(rs.getInt("VIEW"));
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.MessageModel#deleteByPK(int)
	 */
	public void deleteByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("MessageModel.deleteByID"));
		p.setInt(1,id);
		p.executeUpdate();
		p.close();
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.MessageModel#addNew(org.jetnuke.entities.Message)
	 */
	public int addNew(Message msg) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("MessageModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("MessageModel.lastGeneratedMessageID"));
		initNewMsg(msg, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}


	/**
	 * @param msg
	 * @param p
	 */
	protected void initNewMsg(Message msg, PreparedStatement p)throws Exception {
		p.setInt(1,msg.getACTIVE());
		p.setString(2,msg.getCONTENT());
		p.setTimestamp(3,new Timestamp(msg.getDATE().getTime()));
		p.setInt(4,msg.getEXPIRE());
		p.setString(5,msg.getMLANGUAGE());
		p.setString(6,msg.getTITLE());
		p.setInt(7,msg.getVIEW());
		
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.MessageModel#update(org.jetnuke.entities.Message)
	 */
	public void update(Message msg) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("MessageModel.update"));
		p.setInt(1,msg.getACTIVE());
		p.setString(2,msg.getCONTENT());
		p.setTimestamp(3,new Timestamp(msg.getDATE().getTime()));
		p.setInt(4,msg.getEXPIRE());
		p.setString(5,msg.getMLANGUAGE());
		p.setString(6,msg.getTITLE());
		p.setInt(7,msg.getVIEW());
		p.setInt(8,msg.getMID());
		p.executeUpdate();
		p.close();
		
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.MessageModel#selectActive()
	 */
	public List selectActive() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("MessageModel.selectActive"));
		
		ResultSet rs = p.executeQuery();
		List list = processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

}
